#include "inifile.h"
#include <stdio.h>

char* GetIniSectionItem(char* FileName,char* Section,char* Item)
{
	char *value,Sect[30],c;
	char *rt;
	int  offset = 0;
	char linebuf[1024];
	FILE *inifp;

	strcpy(Sect,"[");
	strcat(Sect,Section);
	strcat(Sect,"]");
	
	/*strcpy(Ite,"[");strcat(Ite,Item);strcat(Ite,"]");*/
	if((inifp=fopen(FileName,"rb"))==NULL)
	{
		return (char*)NULL;
	}
	
	/*printf("Sect = %s,Item = %s /n",Sect,Item);*/
	while((c=fgetc(inifp))!=EOF)
	{
		if(c=='[')
		{
			ungetc(c,inifp);
			fgets(linebuf,1024,inifp);
			if(strstr(linebuf,Sect))
			{
				while((c=fgetc(inifp))!='[' && c!=EOF)
				{
					ungetc(c,inifp);
					fgets(linebuf,1024,inifp);
					if(strstr(linebuf,Item))
					{
						value=strchr(linebuf,'=');
						rt = strchr(linebuf, '\r');
						if(!rt)
							rt = strchr(linebuf, '\n');
						if(rt)
						{
							offset= rt - linebuf;
							linebuf[offset]= 0;
						}

						if(value)
						{
							value++;
							/*printf("value = %s /n",value);*/
							fclose(inifp);
							/*'/n' == 0xa*/
							if(*value == 0xa)
								return (char *)NULL;
							return value;
						}
					}
				}
				if(c==EOF)
				{
					break;
				}
				ungetc(c,inifp);
			}
		}
		else
		{
			ungetc(c,inifp);
			fgets(linebuf,1024,inifp);
		}
	}
	
	fclose(inifp);
	return (char*)NULL;
}
